package app.v1.common.controller;

import app.v1.common.domain.ao.dict.DictCreateAo;
import app.v1.common.domain.ao.dict.DictListAo;
import app.v1.common.domain.ao.dict.DictUpdateAo;
import app.v1.common.domain.pojo.Dict;
import app.v1.common.domain.vo.dict.DictListVo;
import app.v1.common.service.DictService;
import com.gitee.feizns.quickstart.domain.web.PageAo;
import com.gitee.feizns.quickstart.domain.web.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.web.Ret;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典类
 * @author feizns
 * @since 2023/2/25
 */
@RestController
@RequestMapping("/api/v1/common/dict")
@RequiredArgsConstructor
public class DictController {

    private final DictService service;

    /**
     * 字典分页
     * @param page 分页参数
     * @param ao 请求参数
     * @return {@link Ret}<{@link PageRecordsVo}<{@link DictListVo}>>
     */
    @GetMapping
    public Ret<PageRecordsVo<DictListVo>> page(PageAo page, DictListAo ao) {
        return Ret.ok(service.page(page, ao));
    }

    /**
     * 创建字典项
     * @param ao 请求参数
     * @return {@link Ret}<{@link Dict}>
     */
    @PostMapping
    public Ret<Dict> create(@Validated @RequestBody DictCreateAo ao) {
        return Ret.ok(service.create(ao));
    }

    /**
     * 字典列表
     * @param ao 请求参数
     * @return {@link Ret}<{@link List}<{@link Dict}>>
     */
    @GetMapping("/list")
    public Ret<List<Dict>> listOfDict(PageAo page, DictListAo ao) {
        return Ret.ok(service.listOfDict(page, ao));
    }

    /**
     * 得到通过编号
     * @param id 编号
     * @return {@link Ret}<{@link Dict}>
     */
    @GetMapping("/{id}")
    public Ret<Dict> getById(@PathVariable  Long id) {
        return Ret.ok(service.getById(id));
    }

    /**
     * 更新通过编号
     * @param id 编号
     * @param ao 请求参数
     * @return {@link Ret}<{@link Dict}>
     */
    @PatchMapping("/{id}")
    public Ret<Dict> updateById(@PathVariable  Long id, @Validated @RequestBody DictUpdateAo ao) {
        return Ret.ok(service.updateById(id, ao));
    }

    /**
     * 删除通过编号
     * @param id 编号
     * @return {@link Ret}<{@link Dict}>
     */
    @DeleteMapping("/{id}")
    public Ret<Dict> deleteById(@PathVariable  Long id) {
        return Ret.ok(service.deleteById(id));
    }

    /**
     * 获取字典
     * @param key 关键
     * @return {@link Ret}<{@link Object}>
     */
    @GetMapping("/{key}/value")
    public Object value(@PathVariable String key, boolean unpack) {
        return unpack ? service.get(key) : Ret.ok(service.get(key));
    }

    /**
     * 更新字典
     * @param key 关键
     * @param value 价值
     * @return {@link Ret}<{@link Object}>
     */
    @Transactional
    @PutMapping("/{key}/value")
    public Ret<Object> putValue(@PathVariable String key, @Validated @RequestBody String value) {
        return Ret.ok(service.put(key, value));
    }

}
